<template>
  <div class="product-list">
    <h1>商品列表</h1>
    <div class="products">
      <div class="product-card" v-for="product in products" :key="product.id">
        <div class="product-info">
          <h3>{{ product.name }}</h3>
          <p class="price">¥{{ product.price }}</p>
          <p class="description">{{ product.description }}</p>
        </div>
        <button class="add-to-cart" @click="addProductToCart(product)">
          加入购物车
        </button>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'

export default {
  name: 'ProductList',
  data() {
    return {
      products: [
        {
          id: 1,
          name: '手机',
          price: 1000,
          description: '最新款智能手机'
        },
        {
          id: 2,
          name: '电脑',
          price: 2000,
          description: '高性能笔记本电脑'
        },
        {
          id: 3,
          name: '键盘',
          price: 200,
          description: '机械键盘'
        }
      ]
    }
  },
  computed: {
    ...mapState(['cart'])
  },
  methods: {
    ...mapMutations(['addProductToCart'])
  }
}
</script>

<style scoped>
.product-list {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.products {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 20px;
  margin-top: 20px;
}

.product-card {
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 15px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  background-color: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.product-info {
  margin-bottom: 15px;
}

h3 {
  margin: 0 0 10px 0;
  color: #2c3e50;
}

.price {
  font-size: 1.2em;
  color: #e74c3c;
  font-weight: bold;
  margin: 5px 0;
}

.description {
  color: #666;
  font-size: 0.9em;
  margin: 5px 0;
}

.add-to-cart {
  background-color: #2ecc71;
  color: white;
  border: none;
  padding: 10px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 1em;
  transition: background-color 0.3s;
}

.add-to-cart:hover {
  background-color: #27ae60;
}
</style> 